<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="pyjm" label="" class="w-72">
          <a-input v-model:value="formState.pyjm" placeholder="姓名/助记码/卡号/手机号/证件证号" allow-clear />
        </a-form-item>
        <a-form-item name="dateRange" label="就诊日期" class="w-80 ml-4">
          <a-range-picker v-model:value="formState['dateRange']" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable fill :loading="loading" column-code="000495" :scroll="{ x: 2600 }" :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'ghzt'">
            <a-tag v-if="record.ghzt === 0" color="blue">候诊中</a-tag>
            <a-tag v-else-if="record.ghzt === 1" color="green">已就诊</a-tag>
            <a-tag v-else-if="record.ghzt === 2" color="red">已退号</a-tag>
            <a-tag v-else-if="record.ghzt === 4" color="red">待回诊</a-tag>
            <a-tag v-else color="green">已完诊</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button v-if="record.ghzt === 0 || record.ghzt === 1" type="link" size="small" @click="onFinish(record, 3)"> 完诊 </a-button>
            <a-button v-if="record.ghzt !== 0 && record.ghzt !== 1 && record.ghzt !== 2" type="link" size="small" @click="onFinish(record, 1)"> 召回 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import useUserStore from '@/store/user'

const { empId } = useUserStore()

const formRef = ref(null)
const formState = reactive({
  dateRange: [dayjs().subtract(20, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  pyjm: ''
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/mzdj/getOutpRecordInfoList',
  formatParams: () => {
    return {
      dateRange: [formState.dateRange?.[0], formState.dateRange?.[1]],
      stdate: formState.dateRange?.[0],
      eddate: formState.dateRange?.[1],
      pyjm: formState.pyjm,
      ghzts: [0, 1, 3, 4],
      type: 0
    }
  }
})
const onFinish = async (row, status) => {
  const { code, msg } = await post('/outp/mzys/modifyPatientClinicStatus', {
    ghzt: status,
    jzbh: row.jzbh,
    ysbh: empId
  })
  if (code !== 0) return message.error(msg)
  message.success(msg || '操作成功')
  getData()
}

onMounted(() => {
  getData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
